package org.shoukaiseki.tools.mybatis.generator.utils.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName SpringContextUtil
 * @Description TODO: springContext工具类
 * @Author Rick
 * @Date 2018/10/10 16:21
 * @Vesion 1.0.0
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    // Spring应用上下文环境
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }


    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    /**
     *
     * @Description TODO: 获取bean
     * @Author Rick
     * @Param: name beanName
     * @Date 2018/10/10 16:23
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }


    /**
     *
     * @Description TODO: 获取bean
     * @Author Rick
     * @Param: name beanName
     * @Date 2018/10/10 16:23
     */
    public static <T> T getBean(Class<T> clazz) throws BeansException {
        return applicationContext.getBean(clazz);
    }
}

